import json

import requests

def piesCategoriesWithSpecials():
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "https://www.woolworths.com.au/",
        "Request-Id": "|6f03dc56c3ba48b3aae88e1c3c958246.2e564bc3f02947fc",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "traceparent": "00-6f03dc56c3ba48b3aae88e1c3c958246-2e564bc3f02947fc-01"
    }
    url = "https://www.woolworths.com.au/apis/ui/PiesCategoriesWithSpecials"
    response = requests.get(url, headers=headers)
    for it in response.json()['Categories']:
        if it['Children'] is not None:
            for c in it['Children']:
                if c['Children'] is not None:
                    for child in c['Children']:
                        print(child)
                        browseCategory(child['NodeId'], child['Description'], 1)


def browseCategory(nodeId, description, pageNumber):
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Origin": "https://www.woolworths.com.au",
        "Pragma": "no-cache",
        "Referer": "https://www.woolworths.com.au/shop/browse/pet/pet-specials?pageNumber=" + str(pageNumber),
        "Request-Id": "|03693e20f7eb4916a2726569b9fd9e84.e4cf1a14a8c44e56",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "traceparent": "00-03693e20f7eb4916a2726569b9fd9e84-e4cf1a14a8c44e56-01"
    }

    url = "https://www.woolworths.com.au/apis/ui/browse/category"
    data = {
        "categoryId": nodeId,
        "pageNumber": pageNumber,
        "pageSize": 36,
        "sortType": "TraderRelevance",
        "url": "/shop/browse/pet/pet-specials?pageNumber=" + str(pageNumber),
        "location": "/shop/browse/pet/pet-specials?pageNumber=" + str(pageNumber),
        "formatObject": "{\"name\":\"" + description + "\"}",
        "isSpecial": True,
        "isBundle": False,
        "isMobile": False,
        "filters": [],
        "token": "",
        "gpBoost": 0,
        "isHideUnavailableProducts": False,
        "isRegisteredRewardCardPromotion": False,
        "enableAdReRanking": False,
        "groupEdmVariants": True,
        "categoryVersion": "v2"
    }
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, data=data)

    print(response.json())


if __name__ == '__main__':
    browseCategory('1_151AB32', 'Other Outdoor Sports', 1)
